package org.abratuhi.kvngroupware.model;

import java.util.ArrayList;

import org.jdom.Element;

public class Team {
	
	String id = new String();
	String name = new String();
	String town = new String();
	String country = new String();
	
	ArrayList<User> members = new ArrayList<User>();
	ArrayList<Joke> jokes = new ArrayList<Joke>();
	
	public Team(){
		
	}
	public Team(String id){
		setId(id);
	}
	public Team(String name, String town, String country){
		setId(name.replace(" ", ""));
		setName(name);
		setTown(town);
		setCountry(country);
	}
	
	public String getId(){
		return this.id;
	}
	public String getName(){
		return this.name;
	}
	public String getTown(){
		return this.town;
	}
	public String getCountry(){
		return this.country;
	}
	/*synchronized*/ ArrayList<User> getMembers(){
		return this.members;
	}
	/*synchronized*/ ArrayList<Joke> getJokes(){
		return this.jokes;
	}
	
	void setId(String id){
		this.id = id;
	}
	void setName(String name){
		this.name = name;
	}
	void setTown(String town){
		this.town = town;
	}
	void setCountry(String country){
		this.country = country;
	}
	
	public synchronized String addMember(User u){
		String returnMessage = "";
		if(this.getMembers().contains(u)){
			returnMessage = "Team already contains member with id:\t"+u.getId()+"."+"\n";
		}
		else{
			this.getMembers().add(u);
			u.setTeam(this.getId());
		}
		return returnMessage;
	}
	
	public synchronized String removeMember(User u){
		String returnMessage = "";
		if(!this.getMembers().contains(u)){
			returnMessage = "Team doesn't contain member with id:\t"+u.getId()+"."+"\n";
		}
		else{
			this.getMembers().remove(u);
			u.setTeam(null);
		}
		return returnMessage;
	}
	public synchronized String removeMember(String id){
		String returnMessage = "";
		if(!this.getMembers().contains(id)){
			returnMessage = "Brainstorm doesn't contain member with id:\t"+id+"."+"\n";
		}
		else{
			Integer index = -1;
			for(int i=0; i<getMembers().size(); i++) if(getMembers().get(i).getId().equals(id)) index = i;
			this.getMembers().get(index).setTeam(null);
			this.getMembers().remove(index);
		}
		return returnMessage;
	}
	
	public synchronized boolean containsMember(User u){
		return this.getMembers().contains(u);
	}
	public synchronized boolean containsMember(String id){
		for(int i=0; i<getMembers().size(); i++){
			if(getMembers().get(i).getId().equals(id)){
				return true;
			}
		}
		return false;
	}
	
	public synchronized String addJoke(Joke j){
		String returnMessage = "";
		if(this.getMembers().contains(j)){
			returnMessage = "Team already contains joke with id:\t"+j.getId()+"."+"\n";
		}
		else{
			this.getJokes().add(j);
			j.setTeamId(this.getId());
		}
		return returnMessage;
	}
	
	public synchronized String removeJoke(Joke j){
		String returnMessage = "";
		if(!this.getMembers().contains(j)){
			returnMessage = "Team doesn't contain joke with id:\t"+j.id+"."+"\n";
		}
		else{
			this.getJokes().remove(j);
			j.setTeamId(null);
		}
		return returnMessage;
	}
	
	public synchronized boolean containsJoke(Joke j){
		return this.getJokes().contains(j);
	}
	
	public Element toElement(){
		Element eTeam = new Element("team");
		
		eTeam.setAttribute("id", getId());
		eTeam.setAttribute("name", getName());
		eTeam.setAttribute("town", getTown());
		eTeam.setAttribute("country", getCountry());
		
		return eTeam;
	}
	
	public static Team fromElement(Element eTeam){
		Team team = new Team();
		
		team.setId(eTeam.getAttributeValue("id"));
		team.setName(eTeam.getAttributeValue("name"));
		team.setTown(eTeam.getAttributeValue("town"));
		team.setCountry(eTeam.getAttributeValue("country"));
		
		return team;
	}

}

	